package com.wangp.mywrite.s_netty.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

import java.nio.charset.StandardCharsets;

/**
 * @author tooStronger
 * @date 2023/4/17
 *
 *
 * <p>
 *  Redis 协议
 * </p>
 */
public class TestHttp {

    public static void main(String[] args) {
        final byte[] line = {13, 10};
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler());
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            ByteBuf buffer = ctx.alloc().buffer();
                            buffer.writeBytes("*3".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            buffer.writeBytes("$3".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            buffer.writeBytes("set".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            buffer.writeBytes("$4".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            buffer.writeBytes("name".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            buffer.writeBytes("$9".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            buffer.writeBytes("moxiurong".getBytes(StandardCharsets.UTF_8));
                            buffer.writeBytes(line);
                            ctx.writeAndFlush(buffer);
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            ByteBuf buf = (ByteBuf) msg;
                            System.out.println(buf.toString(StandardCharsets.UTF_8));
                        }
                    });
                }
            });
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6379).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception ignore) {

        } finally {
            worker.shutdownGracefully();
        }
    }
}
